package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.*;
import com.baitengsoft.patentapimonitor.api.model.param.SearchApplicantAnalysisParam;
import com.baitengsoft.patentapimonitor.api.model.param.SearchApplicantParam;
import com.baitengsoft.patentapimonitor.api.service.*;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 统计分析权利人统计
 *
 * @author Chen 2018/1/5
 */
@RestController
@RequestMapping("/api/v1/web/analysis")
@Api(value = "前台-统计分析-权利人统计", description = "权利人统计")
public class WebApplicantController extends AbstractController {
    @Autowired
    private RptApplicantService rptApplicantService;
    @Autowired
    private WebPlatformareaService webPlatformareaService;
    @Autowired
    private PacIndustryService pacIndustryService;
    @Autowired
    private CfgDictionaryService cfgDictionaryService;

    /**
     * 获取筛选参数
     */
    @ApiOperation(value = "获取筛选参数（webAreaInfos=区域，WebIndustryInfo=行业,CfgDictionaryInfo=权利人类型,DataTypeInfo =数据范围 ）")
    @GetMapping("/ApplicantSearchParam")
    @ResponseBody
    public Res getApplicantSearchParam() {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        //获取区域name和区域ID
        List<WebAreaInfo> webAreaInfos = webPlatformareaService.getNameAndIdByPlatformId(userInfo.getPlatformid());
        //获取行业name和行业code
        List<WebIndustryInfo> webIndustryInfos = pacIndustryService.getIndustryInfoByPlatformId(userInfo.getPlatformid());
        //获取权力人类型和权力人code
        List<CfgDictionaryInfo> cfgDictionaryInfos = cfgDictionaryService.getNameAndValueByCode("1004");
        //获取权力人类型和权力人code
        List<CfgDictionaryInfo> lawStates = cfgDictionaryService.getNameAndValueByCode("1001");
        //获取数据范围
        List<RptApplicantDataTypeInfo> DataTypeInfo = rptApplicantService.getApplicantDataType();
        return Res.ok()
                .put("webAreaInfos", webAreaInfos)
                .put("WebIndustryInfo", webIndustryInfos)
                .put("CfgDictionaryInfo", cfgDictionaryInfos)
                .put("DataTypeInfo", DataTypeInfo)
                .put("lawState", lawStates);
    }

    /**
     * 权利人统计
     */
    @ApiOperation(value = "权利人统计")
    @GetMapping("/applicantAnalysis")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "areaId", value = "区域ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "dataRange", value = "数据范围  1.当前有效量，2，专利总量，3，今年公开量，4，今年授权量", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applicantName", value = "权利人名字", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "industryId", value = "行业Id", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序列", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "type", name = "type", value = "1,企业，2个人，3，院校，4科研机构，5机关团体，6其他", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)

    })
    public Res getApplicantAnalysisList(@ApiIgnore SearchApplicantAnalysisParam param) throws Exception {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        PageResult<WebApplicantInfo> infos = rptApplicantService.getApplicantAnalysisList(userInfo.getPlatformid(), param);
        return Res.ok().put("list", infos);
    }

    /**
     * 获取数据范围
     */
    @ApiOperation(value = "获取数据范围下拉框")
    @GetMapping("/dataType")
    @ResponseBody
    public Res getDataType() {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        //获取数据范围
        List<RptApplicantDataTypeInfo> DataTypeInfo = rptApplicantService.getApplicantDataType();
        return Res.ok().put("DataTypeInfo", DataTypeInfo);
    }

    /**
     * 权力人行业分布
     */
    @ApiOperation(value = "权力人行业分布")
    @GetMapping("/applicantIndustry")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "dataTypeValue", value = "数据类型", required = true)
    })
    public Res getApplicantIndustry(Integer dataTypeValue) {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        if (dataTypeValue == null)
            return Res.error("数据类型不能为空");
        //获取数据范围
        List<WebApplicantIndustryInfo> DataTypeInfo = rptApplicantService.getApplicantIndustry(userInfo.getPlatformid(), dataTypeValue);
        return Res.ok().put("info", DataTypeInfo);
    }

    /**
     * 权力人类型分布
     */
    @ApiOperation(value = "权力人类型分布")
    @GetMapping("/applicantType")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "dataTypeValue", value = "数据类型", required = true)
    })
    public Res getApplicantType(Integer dataTypeValue) {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        if (dataTypeValue == null)
            return Res.error("数据类型不能为空，权力人专利量");
        //获取权力人总数（全部权力人）
        //获取数据范围
        List<WebApplicantTypeInfo> applicantType = rptApplicantService.getApplicantType(userInfo.getPlatformid(), dataTypeValue);
        return Res.ok().put("info", applicantType);
    }

}
